<?php

use yii\db\Migration;

class m170506_022248_create_coupon_code extends Migration
{
    private function createDbs(){
        $dbs = [];
        $dbs_info =\Yii::$app->params['db'];
        foreach($dbs_info as $k=>$v){
            $dbs[$k] = \Yii::createObject($v);
        }
        return $dbs;
    }


    // Use safeUp/safeDown to run migration code within a transaction
    public function safeUp()
    {
        $dbs = $this->createDbs();
        foreach ($dbs as $v) {
            $this->db = $v;
            $this->createTable('{{%coupons_code}}',[
                'id' => $this->primaryKey()->notNull().' COMMENT \'自增id\'',
                'coupons_id' => $this->integer()->defaultValue(0).' COMMENT \'优惠券 id\'',
                'code' => $this->string(50)->defaultValue(null).' COMMENT \'券码\'',
                'salesman_id' => $this->smallInteger(2)->defaultValue(0).' COMMENT \'返券人id\'',
                'status' => $this->smallInteger(2)->defaultValue(0).' COMMENT \'状态（0 未领取 1 已领）\'',
                'is_use' => $this->smallInteger(2)->defaultValue(0) .' COMMENT \'是否使用（0 未使用 1 已使用）\'',
                'create_at' => $this->integer()->defaultValue(0) .' COMMENT \'添加时间\''
            ]);
        }
    }

    public function safeDown()
    {
        $dbs = $this->createDbs();
        foreach($dbs as $v){
            $this->db = $v;
            $this->dropTable('{{%coupons_code}}');
        }
    }

}
